package com.cn.redis;


import com.cn.redis.exception.IDException;
import com.cn.redis.exception.OverStepException;
import com.cn.redis.properties.CommonProperties;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("IdGenerator")
public class IdGenerator {
    @Resource(
            name = "com.cn.redis.properties.cp"
    )
    CommonProperties commonProperties;
    @Resource(
            name = "com.cn.redis.gen.a"
    )
    private com.cn.redis.service.impl.a a;
    @Resource(
            name = "com.cn.redis.gen.b"
    )
    private com.cn.redis.service.impl.b b;

    public IdGenerator() {
    }

    public Long next(String key) {
        key = this.commonProperties.getKeyFull(key);
        if ("default".equals(this.commonProperties.getType())) {
            return this.a.e(key);
        } else if ("sf".equals(this.commonProperties.getType())) {
            return this.b.d(key);
        } else {
            throw new UnsupportedOperationException(this.commonProperties.getType() + "  not supported!");
        }
    }

    public List<Object> nextList(String key, int size) throws IDException {
        if (size > this.commonProperties.getStepC(key)) {
            throw new OverStepException();
        } else {
            key = this.commonProperties.getKeyFull(key);
            if ("default".equals(this.commonProperties.getType())) {
                return this.a.nextList(key, size);
            } else if (!"sf".equals(this.commonProperties.getType())) {
                throw new UnsupportedOperationException(this.commonProperties.getType() + "  not supported!");
            } else {
                List<Object> currentList = new ArrayList();

                for(int i = 0; i < size; ++i) {
                    currentList.add(this.b.d(key));
                }

                return currentList;
            }
        }
    }
}

